package com.cqu.hospitalsystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.cqu.hospitalsystem.Vo.preMedicineVo;
import com.cqu.hospitalsystem.entity.Doctor;
import com.cqu.hospitalsystem.entity.Medicine;
import com.cqu.hospitalsystem.service.MedicineService;
import com.cqu.hospitalsystem.utils.result.DataResult;
import com.cqu.hospitalsystem.utils.result.code.Code;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("medicine")
@Tag(name = "MedicineController", description = "药品管理")
public class MedicineController {
    /**
     * 服务对象
     */
    @Resource
    private MedicineService medicineService;

    @PostMapping("allMedicine")
    @Operation(summary = "获取所有药品", description = "查询所有药品")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到药品")
    })
    public DataResult allMedicine() {
        List<Medicine> list = this.medicineService.allMedicine();
        JSONObject jsonObject = new JSONObject();
        if (list != null) {
            jsonObject.put("medicineList", list);
            DataResult dataResult = DataResult.successByData(jsonObject);
            dataResult.setcode(666);
            return dataResult;
        } else {
            return DataResult.errByErrCode(Code.ACCOUNT_ERROR);
        }
    }

    @GetMapping
    @Operation(summary = "分页查询药品", description = "根据筛选条件和分页信息查询药品")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "400", description = "查询失败")
    })
    public ResponseEntity<Page<Medicine>> queryByPage(Medicine medicine, PageRequest pageRequest) {
        return ResponseEntity.ok(this.medicineService.queryByPage(medicine, pageRequest));
    }

    @PostMapping("chooseMedicine")
    @Operation(summary = "选择药品", description = "根据药品ID查询药品")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "药品不存在")
    })
    public DataResult chooseMedicine(@RequestBody @Parameter(description = "药品对象") Medicine medicine) {
        System.out.println("chooseMedicine");
        System.out.println(medicine.getMedicineId());
        Medicine medicine1 = this.medicineService.queryById(medicine.getMedicineId());
        if (medicine1 != null) {
            DataResult dataResult = DataResult.successByData(medicine1);
            dataResult.setcode(666);
            return dataResult;
        } else {
            return DataResult.errByErrCode(Code.ACCOUNT_ERROR);
        }
    }

    @GetMapping("{id}")
    @Operation(summary = "根据ID查询药品", description = "根据药品ID查询单个药品")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "药品不存在")
    })
    public ResponseEntity<Medicine> queryById(@PathVariable("id") @Parameter(description = "药品ID") Long id) {
        return ResponseEntity.ok(this.medicineService.queryById(id));
    }

    @PostMapping
    @Operation(summary = "新增药品", description = "创建新的药品")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "创建失败")
    })
    public ResponseEntity<Medicine> add(@RequestBody @Parameter(description = "药品对象") Medicine medicine) {
        return ResponseEntity.ok(this.medicineService.insert(medicine));
    }

    @PutMapping
    @Operation(summary = "编辑药品", description = "更新药品信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "更新失败")
    })
    public ResponseEntity<Medicine> edit(@RequestBody @Parameter(description = "药品对象") Medicine medicine) {
        return ResponseEntity.ok(this.medicineService.update(medicine));
    }

    @DeleteMapping
    @Operation(summary = "删除药品", description = "根据药品ID删除药品")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "删除失败")
    })
    public ResponseEntity<Boolean> deleteById(@Parameter(description = "药品ID") Long id) {
        return ResponseEntity.ok(this.medicineService.deleteById(id));
    }

    @PostMapping("cancelMedicine")
    @Operation(summary = "退药", description = "根据处方单ID和药品ID退药")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "退药成功"),
            @ApiResponse(responseCode = "400", description = "退药失败")
    })
    public DataResult cancelMedicine(@RequestBody @Parameter(description = "退药信息") preMedicineVo preMedicineVo) {
        System.out.println("这是要取消的药品");
        System.out.println("取消处方单ID：" + preMedicineVo.getPrescriptionId());
        System.out.println("这是药品ID：" + preMedicineVo.getMedicineId());
        System.out.println(preMedicineVo);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("isSuccess", this.medicineService.cancelMedicine(preMedicineVo));
        return DataResult.successByData(jsonObject);
    }

    @PostMapping("cancelMedicineByPre")
    @Operation(summary = "根据处方单退药", description = "根据处方单ID退药")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "退药成功"),
            @ApiResponse(responseCode = "400", description = "退药失败")
    })
    public DataResult cancelByPre(@RequestBody @Parameter(description = "退药信息") preMedicineVo preMedicineVo) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("isSuccess", this.medicineService.cancelMedicineByPre(preMedicineVo));
        return DataResult.successByData(jsonObject);
    }
}